package com.zyb.system.menu.controller;

import com.zyb.system.menu.service.IMenuService;
import com.zyb.system.menu.domain.Menu;
import com.zyb.system.menu.query.MenuQuery;
import com.zyb.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(value = "菜单的API",description="菜单相关的CRUD功能")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "新增菜单【如有id为修改操作】")
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResult.success().setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("保存失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "根据id删除一条数据")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            menuService.remove(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg("删除失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "查询菜单的一条数据")
    public Menu queryById(@PathVariable("id")Long id)
    {
        return menuService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping("/list")
    @ApiOperation(value = "部门查询所有数据")
    public List<Menu> queryAll(){

        return menuService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pageList")
    @ApiOperation(value = "菜单分页查询")
    public AjaxResult pageList(@RequestBody MenuQuery query){
        try {
            List<Menu> page = menuService.queryPage(query);
            Long count = menuService.queryCount(query);
            return AjaxResult.success().setData(page).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return AjaxResult.error().setMsg("获取分页数据失败");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody  List<Long> ids){
        try {
            menuService.batchDelete(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    @ApiOperation(value = "菜单树")
    @GetMapping("/getTree")
    public List<Menu> getTree(){
        return menuService.getMenuTree();
    }

}
